package com.lucas.controller;

import com.lucas.exception.MyException;
import com.lucas.service.AuthCodeService;
import com.lucas.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: spring_cloud_lagou_homework
 * @author: lucasLu
 **/
@RestController
public class CodeController {
    @Autowired
    AuthCodeService authCodeService;

    /**
     *校验验证码是否正确 0 正确 1错误  2 超时
     * @param email
     * @param code
     * @return
     */
    @RequestMapping("/validateCode/{email}/{code}")
    Integer validateCode(@PathVariable  String email, @PathVariable String code){
        return authCodeService.validate(email, code);
    }


    /**
     * 生成验证码并发送到对应邮箱
     * @param email
     * @return
     */
    @RequestMapping("/create/{email}")
    public Result createCode(@PathVariable String email){
        boolean flag = false;
        try {
            flag = authCodeService.createAndSendCode(email);
        }catch (MyException e){
            return Result.error(e.getMsg());
        }
        return Result.ok().put("data",flag);


    }
}
